local blaze = require "blaze"
local app = require "app"

local log = app.logging.get("Footer")

local M = app.ui.element()

M.store_watchers = {
    avatar = {
        ["等级变更"] = function(self, store, state, args)
            self.level_txt.text = tostring(state.level)
        end,
        ["昵称变更"] = function(self, store, state, args)
            self.name_txt.text = state.name
        end,
        ["头像变更"] = function(self, store, state, args)
            self.avatar_icon:set_head(state.head)
        end,
        ["国家变更"] = function(self, store, state, args)
            self.avatar_icon:set_flag(state.country)
        end,
        ["筹码变更"] = function(self, store, state, args)
            local style = app.i18n.styles.chip2
            app.utility.digit(self.chip_txt, args.old, state.chip, style)
        end,
        ["钻石变更"] = function(self, store, state, args)
            app.utility.digit(self.diamond_txt, args.old, state.diamond)
        end,
        ["经验变更"] = function(self, store, state, args)
            self.xp_slider.value = state.exp / state.max_exp
        end,
        ["经验上限变更"] = function(self, store, state, args)
            self.xp_slider.value = state.exp / state.max_exp
        end
    }
}

function M:ctor()
    local AvatarIcon = require "ui.Common.AvatarIcon"
    self.avatar_icon = AvatarIcon.new()
end

function M:get_chip_transform()
    return self.loaded and self.chip_root.transform or nil
end

function M:on_set_gameobject()
    self:super("on_set_gameobject")
    self.loaded = true
end

function M:on_injected(bind)
    bind("gameobject.element", self.avatar_icon_obj, self.avatar_icon)
    bind("button.click", self.setting_btn, self.on_setting)
    bind("button.click", self.achievement_btn, self.on_achievement)
    bind("button.click", self.message_btn, self.on_message)
    bind("button.click", self.leaderboard_btn, self.on_leaderboard)
    bind("button.click", self.feedback_btn, self.on_feedback)
    bind("button.click", self.profile_btn, self.on_profile)
end

function M:on_profile()
    local user_id = app.kbe.player().user_id
    app.store.profile.query_user_detail_view({user_id = user_id})
    :next(function()
        app.router.push("/profile")
    end)
end

function M:on_leaderboard()
    app.audio.post("lobby:button down")
    app.router.push("/leaderboard")
end

function M:on_setting()
    app.audio.post("lobby:button down")
    app.router.push("/setting")
end

function M:on_achievement()
    app.audio.post("lobby:button down")
    app.router.push("/achievement")
end

function M:on_message()
    app.audio.post("lobby:button down")
    app.router.push("/mail")
end

function M:on_feedback()
    app.audio.post("lobby:button down")
    app.router.push("/feedback")
end

function M:on_release()
    self.avatar_icon:release()
end

return M